package main

import (
	"fmt"
)

func main() {
	key := "GOLANG"
	str := "CSOITEUIWUIZNSROCNKFD"

	fmt.Printf(decode(str, key) + "\n")
	fmt.Printf(encode(decode(str, key), key))

}

func decode(str string, key string) string {
	ans := ""
	keyArr := dekey(key)
	lens := len(key)

	for idx, c := range str {
		i := idx % lens
		if c-rune(keyArr[i]) < 65 {
			ans += string('Z' - 64 + c - rune(keyArr[i]))
		} else {
			ans += string(c - rune(keyArr[i]))
		}
	}
	return ans
}

func encode(str string, key string) string {
	ans := ""
	keyArr := dekey(key)
	lens := len(key)

	for idx, c := range str {
		i := idx % lens
		if c+rune(keyArr[i]) > 90 {
			ans += string('A' + c + rune(keyArr[i]) - 91)
		} else {
			ans += string(c + rune(keyArr[i]))
		}
	}
	return ans
}

func dekey(key string) []int {
	keyArr := make([]int, len(key))
	for idx, k := range key {
		keyArr[idx] = int(k) - 65
	}
	return keyArr
}
